package cn.suhaoblog.oss.controller;

import cn.suhaoblog.core.door.CommonResponse;
import cn.suhaoblog.oss.entity.OSSFile;
import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectRequest;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author rambler
 * @since 2020-12-01 11:19
 */
@RestController
@RequestMapping("/api/file")
@Api(tags = {"通用文件上传"}, protocols = "HTTP", authorizations = @Authorization("Authorization"))
public class OSSFileController {
    @Resource
    private OSS ossClient;

    // 访问自定义域名
    private static final String domain = "https://rambler-blog.oss-cn-beijing.aliyuncs.com";

    // 项目级bucket名称, 整个项目统一只使用这一个, 不同分类使用文件夹区分
    public static final String GLOBAL_BUCKET_NAME = "common-bucket-in-one";

    @ApiOperation(value = "简单文件上传")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "file", value = "上传的文件", required = true, paramType = "formData", dataType = "file"),
//            @ApiImplicitParam(name = "folder", value = "上传到oss的文件夹名称", required = true, paramType = "query", dataType = "string")
//    })
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public CommonResponse<OSSFile> insert(@RequestParam("file") MultipartFile file, @RequestParam("folder") String folder) {
        try {
            // 上传的文件名
            String fileName = file.getOriginalFilename();
            String key = folder + "/" + file.getOriginalFilename();
            ossClient.putObject(new PutObjectRequest(GLOBAL_BUCKET_NAME, key, file.getInputStream()));
            // 保存到domain/{folder}/{file}
            String url = domain + "/" + folder + "/" + fileName;
            // 返回OSSFile对象，包含常用属性
            OSSFile result = new OSSFile();
            result
                    .setOriginalName(fileName)
                    .setUrl(url)
                    .setName(fileName)
                    .setFolder(folder)
                    .setBucketName(GLOBAL_BUCKET_NAME).end();
            return new CommonResponse<OSSFile>().ok().msg("上传成功").data(result);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return new CommonResponse<OSSFile>().fail().msg("上传失败");
        }
    }

    @ApiOperation(value = "删除上传的图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileName", value = "文件名", required = true, paramType = "query"),
            @ApiImplicitParam(name = "folder", value = "上传到oss的文件夹名称", required = true, paramType = "query")
    })
    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    public CommonResponse<String> delete(@RequestParam("fileName") String fileName, @RequestParam("folder") String folder) {
        try {
            // 上传的文件名
            String objectName = folder + "/" + fileName;
            ossClient.deleteObject(GLOBAL_BUCKET_NAME, objectName);
            return new CommonResponse<String>().ok().msg("删除成功");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return new CommonResponse<String>().fail().msg("删除失败");
        }
    }

}
